import { GaRegExp, ruleInjectable, ruleInject, RuleInfo } from "../InjectionRule";
import { blankRemove } from "../methods/blank-remove";

@ruleInjectable()
export default class unaryRule extends GaRegExp {

	@ruleInject() private dataRule!: GaRegExp;

	public RuleInit() {
		this.ParseRule([
			this.Supply("UNARY"),
			"(&",
			"(!", "[\\!\\~\\-]", "|", "(-", "\\+", this.preHandle("+"), "\\+", "|", "\\-", this.preHandle("-"), "\\-", ")", ")", "|",
			"(-",
			"(!", "new", "|", "default", ")", "\\s", blankRemove, ")",
			")?",
			this.dataRule
		]);
	}

	private preHandle = (operation: string): RuleInfo => ({
		ruleType: "position",
		ruleData: (save, origin, rule, str, index, char, length) => {
			for (let i = index - 1; i > 0; i--) {
				const temp = str[i];
				if (temp === "string") {
					continue;
				} else if (temp === "end") {
					continue;
				} else if (temp === "list") {
					continue;
				} else {
					if (operation === temp) {
						if (operation === str[i - 1]) {
							return false;
						} else {
							return true;
						}
					}
				}
			}
			return false;
		},
	});

}
